//
//  BIZContractFileListVC.m
//  biz
//
//  Created by 小龙 on 2017/4/19.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZContractFileListVC.h"
#import "FileOperation.h"

@interface BIZContractFileListVC ()
@property (nonatomic, strong) NSMutableArray *fileArr;
@property (nonatomic, copy) NSString *docFilePath;
@end

@implementation BIZContractFileListVC

- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.title = @"请选择合同文件";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon-denglu-fanhui副本"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemDidClicked)];
    
    [self getFileData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.fileArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cellID";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    cell.textLabel.text = self.fileArr[indexPath.row];
    
    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *docFilePath = [self.docFilePath stringByAppendingPathComponent:self.fileArr[indexPath.row]];
//    NSData *reader = [NSData dataWithContentsOfFile:path];
    if (self.selecteFileReturnBlock) {
        self.selecteFileReturnBlock(docFilePath);
    }
    [self dismissViewControllerAnimated:YES completion:nil];

}




- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    DeLog(@"commitEditingStyle %ld", (long)editingStyle);
    
}

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSMutableArray *actionArr = [NSMutableArray array];
    //移除
    UITableViewRowAction *removeAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        //事件
        [self deleteFileWithIndexPath:indexPath];
        
    }];
    
    [actionArr addObject:removeAction];
    return actionArr;
}



- (void)deleteFileWithIndexPath:(NSIndexPath *)indexPath
{
    // UIAlertViewController(弹框视图)
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确认删出吗" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        //        NSString *docFilePath = [self.docFilePath stringByAppendingPathComponent:self.fileArr[indexPath.row]];
        ////
        [FileOperation removeFile:self.fileArr[indexPath.row] AtPath:self.docFilePath];
        //
        //        [self.fileArr removeObject:docFilePath];
        
        //        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationAutomatic];  //删除对应数据的cell
        [self getFileData];
        
    }];
    [alert addAction:confirm];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:cancel];
    
    
    
    [self presentViewController:alert animated:YES completion:nil];
}


- (void)leftBarButtonItemDidClicked
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


-(void)getFileData
{
    NSArray *arr = [FileOperation getAllFileName:self.docFilePath];
    
    DeLog(@"遍历文件：%@",arr);
    
    for (NSString *str in arr) {
        NSLog(@"所有名称%@",str);
        if ([str containsString:@".doc"]||[str containsString:@".docx"]) {
            [self.fileArr addObject:str];
        }
        
    }
    
    [self.tableView reloadData];
}


- (NSMutableArray *)fileArr
{
    if (!_fileArr) {
        _fileArr = [NSMutableArray array];
    }
    return _fileArr;
}

- (NSString *)docFilePath
{
    if (!_docFilePath) {
        _docFilePath = [FileOperation getDocumentsPath]; 
    }
    
    return _docFilePath;
}

@end
